﻿
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using WinFormsApp1.Leo;
using WinFormsApp1.SuYi.week1WinForm;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
using static WinFormsApp1.SuYi.week1WinForm.BusinesWinFormShow;
using Image = System.Drawing.Image;

namespace WinFormsApp1.WangHaoQi.Week02Winfroms
{
    public partial class CheckInAddHtml : Form
    {
        public CheckInAddHtml()
        {
            InitializeComponent();
            SupplierShow();
            SettlementTypeShow();
        }
        /// <summary>
        /// 供应商下拉框绑定
        /// </summary>
        public async void SupplierShow()
        {
            var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<Suppliers>>>(URLCommon.Url2, "/CheckIn/SupplierShow", HttpClientHelper.MethodType.POST, null);
            uiComboBox3.DataSource = items.Data;
            uiComboBox3.DisplayMember = "SupplierName";
            uiComboBox3.ValueMember = "SupplierId";

            this.Controls.Add(uiComboBox3);
        }
        /// <summary>
        /// 供应商
        /// </summary>
        public class Suppliers
        {
            public int SupplierId { get; set; }
            public string SupplierName { get; set; }
        }
        /// <summary>
        /// 进场类型下拉框绑定
        /// </summary>
        public async void SettlementTypeShow()
        {
            var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<SettlementType>>>(URLCommon.Url2, "/CheckIn/SettlementTypeShow", HttpClientHelper.MethodType.POST, null);
            uiComboBox2.DataSource = items.Data;
            uiComboBox2.DisplayMember = "SettlementTypeName";
            uiComboBox2.ValueMember = "SettlementTypeId";

            this.Controls.Add(uiComboBox2);
        }
        /// <summary>
        /// 进场类型
        /// </summary>
        public class SettlementType
        {
            public int SettlementTypeId { get; set; }
            public string SettlementTypeName { get; set; }
        }

        /// <summary>
        /// 进场登记按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void uiButton2_Click(object sender, EventArgs e)
        {
            CheckIn checkIn = new CheckIn()
            {
                CheckInNo = DateTime.Now.ToString().Replace("/", "").Replace(" ", "").Replace(":", ""),
                EnterTime = Convert.ToDateTime(uiDatetimePicker1.Text),
                GoOutTime = Convert.ToDateTime("2000-01-01"),
                CarNo = uiTextBox1.Text,
                SupplierId = Convert.ToInt32(uiComboBox3.SelectedValue),
                SettlementTypeId = Convert.ToInt32(uiComboBox2.SelectedValue),
                ApproachNum = Convert.ToInt32(uiTextBox3.Text),
                QuarantineNo = uiTextBox4.Text,
                OriginAddr = uiTextBox7.Text,
                CheckInBatch = uiTextBox2.Text,
                Deliveryman = uiTextBox12.Text,
                Phone = uiTextBox9.Text,
                IdentityCard = uiTextBox6.Text,
                EnterRegistrar = uiTextBox5.Text,
                GoOutRegistrar = "",
                QuarantineImg = imagePath,
                CheckInStateId = 2,
                Remark = uiTextBox10.Text,
                CheckInIsDel = false
            };
            var res = await HttpClientHelper.RequestUrl<CheckIn, ApiResult<CheckIn>>(URLCommon.Url1, "/CheckIn/CheckInAdd", HttpClientHelper.MethodType.POST, checkIn);
            if (res.Code == 1)
            {
                //弹框提示
                MessageBox.Show("添加成功");
                //创建新页面
                //BusinesWinFormShow queryshow = new BusinesWinFormShow();
                //显示 创建的新页面
                //queryshow.Show();
                //关闭本页面
                this.Hide();
            }
        }
        public class CheckIn
        {
            /// <summary>
            /// 进场登记id
            /// </summary>
            public int CheckInId { get; set; }
            /// <summary>
            /// 进场登记编号
            /// </summary>
            public string CheckInNo { get; set; }
            /// <summary>
            /// 进场登记时间
            /// </summary>
            public DateTime EnterTime { get; set; }
            /// <summary>
            /// 出场时间
            /// </summary>
            public DateTime GoOutTime { get; set; }
            /// <summary>
            /// 车牌号
            /// </summary>
            public string CarNo { get; set; }
            /// <summary>
            /// 供应商/养殖户 外健
            /// </summary>
            public int SupplierId { get; set; }
            /// <summary>
            /// 结算类型 外键
            /// </summary>
            public int SettlementTypeId { get; set; }
            /// <summary>
            /// 进场数量
            /// </summary>
            public int ApproachNum { get; set; }
            /// <summary>
            /// 检疫证号
            /// </summary>
            public string QuarantineNo { get; set; }
            /// <summary>
            /// 产地
            /// </summary>
            public string OriginAddr { get; set; }
            /// <summary>
            /// 进场批次
            /// </summary>
            public string CheckInBatch { get; set; }
            /// <summary>
            /// 送货联系人
            /// </summary>
            public string Deliveryman { get; set; }
            /// <summary>
            /// 联系电话
            /// </summary>
            public string Phone { get; set; }
            /// <summary>
            /// 身份证
            /// </summary>
            public string IdentityCard { get; set; }
            /// <summary>
            /// 进场登记员
            /// </summary>
            public string EnterRegistrar { get; set; }
            /// <summary>
            /// 出场登记员
            /// </summary>
            public string GoOutRegistrar { get; set; }
            /// <summary>
            /// 检疫证（图片）    上传
            /// </summary>
            public string QuarantineImg { get; set; }
            /// <summary>
            /// 进场登记状态       外键
            /// </summary>
            public int CheckInStateId { get; set; } = 0;
            /// <summary>
            /// 备注
            /// </summary>
            public string Remark { get; set; }
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool CheckInIsDel { get; set; }
        }
        public string imagePath { get; set; }
        private void pictureBoxImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图片文件(*.jpg;*.jpeg;*.gif;*.bmp;*.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    imagePath = "";
                    ///新的图片路径
                    imagePath = openFileDialog.FileName;
                    //并显示出来
                    pictureBoxImage.Image = Image.FromFile(imagePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("上传出错: " + ex.Message);
                }
            }

        }
    }
}
